﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Webbshop.ServiceReference1;

namespace Webbshop
{
    public partial class overViewPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var targetCategory = Request.QueryString["category"];
           

            if (!IsPostBack)
            {
                ShoppeServiceClient proxy = new ShoppeServiceClient();
                try{
                    if (targetCategory != null){
                        CategoryResultList.DataSource = proxy.getTargetCategory(targetCategory);
                    }
                    else {
                        CategoryResultList.DataSource = proxy.GetProducts();
                    }
                    CategoryResultList.DataBind();
                }
                finally{ proxy.Close(); }
            }
        }

        /// <summary>
        /// Event som sker när en användare interagerar med DataList:en "CategoryResultList"
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void Item_Command(object source, RepeaterCommandEventArgs e)
        {
            Session["ShoppingCart"] = Functions.GetCart(Session["ShoppingCart"] as Cart); // Hämta/Skapa Varukorg
            Functions.AddToCart(Session["ShoppingCart"] as Cart, e.CommandArgument.ToString(), e.Item.ItemIndex);
            // Genomför Postback
            Response.Redirect(Request.Url.AbsolutePath + Request.Url.Query);
        }
    }
}